package com.dyh.quickdemozwei.bingfa0916;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

public class Bingfa0916 {


    @AllArgsConstructor@Setter@Getter
    public static class Cmd{
        String eid;
        String pid1;
        String pid2;
    }

    @Setter@Getter
    public static class Polymerization{
         Set<String> codes = new HashSet<>();
         List<Cmd> cmds = new ArrayList<>();

         public void add(Cmd cmd){
             codes.add(cmd.pid1);
             codes.add(cmd.pid2);
             cmds.add(cmd);
         }
    }

    public static void main(String[] args) {
        List<Cmd> cmds = Lists.newArrayList(
                new Cmd("e1","p1","p2"),
                new Cmd("e2","p1","p3"),
                new Cmd("e3","p4","p5"),
                new Cmd("e4","p5","p3"),
                new Cmd("e5","p6","p7")
        );

        Polymerization[] polymerizations = new Polymerization[cmds.size()];

        Consumer<Cmd> mergeCmd = e->{
              for (int i =0;i<polymerizations.length;){
                  Polymerization polymerization = polymerizations[i];
                  if (polymerization==null){
                      polymerization = new Polymerization();
                      polymerization.add(e);
                      polymerizations[i]=polymerization;
                      break;
                  }
                  else{
                      if (polymerization.codes.contains(e.pid1)||polymerization.codes.contains(e.pid2)){
                          //可以合并
                          polymerization.add(e);
                          break;
                      }
                      else {
                          i++;
                      }
                  }
              }
        };

        for (Cmd cmd : cmds) {
            mergeCmd.accept(cmd);
        }


        System.out.println();

        for (;;){
            boolean hasMerge =false;
            for (int i=0;i<polymerizations.length-1;i++){
                Polymerization p1 = polymerizations[i];
                if (p1==null)
                    continue;
                for (int j=i+1;j<polymerizations.length;j++){
                    Polymerization p2 = polymerizations[j];
                    if (p2==null)
                        continue;

                    boolean mergeP1P2 = false;
                    for (String code : p1.codes) {
                        if (p2.codes.contains(code)){
                            mergeP1P2 = true;
                            break;
                        }
                    }
                    if (hasMerge=mergeP1P2){
                        p1.codes.addAll(p2.codes);
                        p1.cmds.addAll(p2.cmds);

                        polymerizations[j]=null;
                    }
                }





            }
            if (!hasMerge)
                break;
        }

        System.out.println(JSON.toJSONString(mergeCmd));

    }



}
